package com.wwg.design.patten.observer.case1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @类描述: TODO
 * @Author wwg
 * @date: 2021/7/20 18:49
 * @Version: 1.0
 **/
public abstract class AbstractSubject implements ISubject{

    /**
     * 该主题的订阅者列表（即观察者）
     */
    private List<IObserver> observers = new ArrayList<>();

    public abstract void someThingHappen(String someThingHappen);

    /**
     * @类描述: 通知观察者
     * @Author wwg
     * @Date: 2021/7/20 18:50
     * @Params []
     * @Return
     */
    public void notifyAllObserver(String someThingHappen){
        for (IObserver observer : observers) {
            notifyObserver(observer,someThingHappen);
        }
    }

    private void notifyObserver(IObserver observer, String someThingHappen) {
        observer.call(someThingHappen);
    }

    @Override
    public void addObserver(AbstractObserver observer){
        observers.add(observer);
    };

    /**
     *
     * @param arg
     */
    @Override
    public void addObserver(IObserver... arg){
        observers.addAll(Arrays.stream(arg).collect(Collectors.toList()));
    };

}
